<?php

$current_page = get_smarty();

$current_page->assign( 'logged', $User->is_logged() );

switch ( @$URL[1] )
{
	case "":
	default:
	
		break;
	
	case "update":
		$User->set_value( 'username', $_POST['username']);
		$User->set_value( 'firstname', $_POST['first_name']);
		$User->set_value( 'lastname', $_POST['last_name']);
		$User->set_value( 'gender', $_POST['gender']);
		$User->set_value( 'address', $_POST['address']);
		$User->set_value( 'city', $_POST['city']);
		$User->set_value( 'country', $_POST['country']);
		$User->set_value( 'website', $_POST['website']);
		$User->set_value( 'email', $_POST['email']);
		$User->set_value( 'comments', $_POST['comment']);
		
		$User->set_value( 'birthdate', $_POST['birth_year'].'-'.$_POST['birth_month'].'-'.$_POST['birth_day']);
				
		if ( !empty( $_FILES['avatar']['tmp_name'] ) )	
		{
			$avatar_error = false;
			$max_height = 200;
			$max_width = 200;
			
			$image_size = getimagesize( $_FILES['avatar']['tmp_name'] );
			$image_x = $image_size[0];
			$image_y = $image_size[1];
			if ( ( $image_x > 0 ) && ( $image_y > 0 ) )
			{
				$ext_parts = explode( ".", $_FILES['avatar']['name'] );
				$ext = $ext_parts[ count( $ext_parts ) - 1 ];
				switch( $ext )
				{
					case 'jpg':
					case 'jpeg':
					case 'jpe':
						$image_temp = imagecreatefromjpeg( $_FILES['avatar']['tmp_name'] );
						break;
					case 'png':
						$image_temp = imagecreatefrompng( $_FILES['avatar']['tmp_name'] );
						break;
					case 'gif':
						$image_temp = imagecreatefromgif( $_FILES['avatar']['tmp_name'] );
						break;
					default: 
						$avatar_error = 'IMAGE_TYPE_NOT_SUPPORTED';
						break;
				}
				$image_avatar = imagecreatetruecolor( $max_width, $max_height );
				imagecopyresampled( $image_avatar, $image_temp, 0, 0, 0, 0, $max_width, $max_height, $image_x, $image_y );
				imagejpeg( $image_avatar, $_FILES['avatar']['tmp_name'], 100 );
			}
			else
			{
				$avatar_error = true;
			}
			
			if ( !$avatar_error )
			{
				move_uploaded_file( $_FILES['avatar']['tmp_name'], $_base_path .'/avatars/'. $User->get_id() .'.jpg' );
				$User->set_value( 'avatar', '1' );		
			}
			
		}
		
		$User->submit_values();
		break;
	
	case "changepassword":

		$old_password = $_POST['old_password'];
		$new_password = $_POST['new_password'];
		$rep_password = $_POST['rep_password'];
		
		if ($new_password == $rep_password) 
		{
			if (! $User->change_password( $old_password, $new_password)) 
			{	
				echo $User->ERROR;
			}
		}
		break;
}

$months = array( 1=>'Jan', 2=>'Feb', 3=>'Mar', 4=>'Apr', 5=>'May', 6=>'Jun', 7=>'Jul', 8=>'Aug', 9=>'Sep', 10=>'Oct', 11=>'Nov', 12=>'Dec' );

$User->get_database_row( true );

$current_page->assign( 'username', $User->get_value('username') );
$current_page->assign( 'first_name', $User->get_value('firstname') );
$current_page->assign( 'last_name', $User->get_value('lastname') );

$current_page->assign( 'months', $months );
$birth = explode( '-', $User->get_value('birthdate'));

$current_page->assign( 'birth_day', @$birth[2] );
$current_page->assign( 'birth_month', @$birth[1] );
$current_page->assign( 'birth_year', @$birth[0] );

$current_page->assign( 'gender', $User->get_value('gender') );
$current_page->assign( 'address', $User->get_value('address') );
$current_page->assign( 'city', $User->get_value('city') );
$current_page->assign( 'country', $User->get_value('country') );
$current_page->assign( 'website', $User->get_value('website') );
$current_page->assign( 'email', $User->get_value('email') );

$registered = explode( ' ', $User->get_value('registerdate') );

$current_page->assign( 'joined', $registered[0]);
$current_page->assign( 'comment', $User->get_value('comments') );
$current_page->assign( 'penalties', 'No penalties' );

$current_page->assign( 'avatar', $User->get_value( 'avatar' ) );
$current_page->assign( 'avatar_file', $User->get_id() .'.jpg' );

$_page_content .= $current_page->fetch( 'myprofile.tpl' );

?>

